﻿using Dapper;
using System;
using System.Text;
using System.Collections.Generic;
using Eagles.Base.DataBase;
using Eagles.DomainService.Model.Product;
using Eagles.Interface.DataAccess.ProductAccess;
using Eagles.Application.Model.Product.GetProduct;

namespace Ealges.DomianService.DataAccess.ProductData
{
    public class ProductDataAccess : IProductAccess
    {
        private readonly IDbManager dbManager;

        public ProductDataAccess(IDbManager dbManager)
        {
            this.dbManager = dbManager;
        }

        public int AddUniqueUser(int userId)
        {
            return dbManager.Excuted(@"insert into eagles.tb_unique_user (EquipDate,UserId) values (@EquipDate,@UserId)", new {EquipDate = DateTime.Now, UserId = userId});
        }

        public int DeleteUniqueUser(int userId)
        {
            return dbManager.Excuted(@"delete from eagles.tb_unique_user where UserId = @UserId ", new { UserId = userId });
        }

        public int EditStock(int saleCount, int prodId)
        {
            return dbManager.Excuted(@"update eagles.tb_product set Stock = Stock - @Stock, SaleCount = SaleCount + @SaleCount 
where (Stock >= @saleCount) and ProdId = @ProdId", new {Stock = saleCount, SaleCount = saleCount, ProdId = prodId});
        }

        public List<TbProduct> GetProduct(GetProductRequest requset)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();
            parameter.Append(" and OrgId = @OrgId ");
            dynamicParams.Add("OrgId", requset.AppId);
            parameter.Append(" and Status = @Status ");
            dynamicParams.Add("Status", 0);
            if (!string.IsNullOrWhiteSpace(requset.ProductName))
            {
                parameter.Append(" and ProdName like @ProdName ");
                dynamicParams.Add("ProdName", "%" + requset.ProductName + "%");
            }
            int pageIndexParameter = (requset.PageIndex - 1) * requset.PageSize;
            dynamicParams.Add("PageIndex", pageIndexParameter);
            dynamicParams.Add("PageSize", requset.PageSize);
            sql.AppendFormat(@"select ProdId,ProdName,Score,ImageUrl from eagles.tb_product where 1=1 {0} order by CreateTime desc limit @PageIndex, @PageSize ", parameter);
            return dbManager.Query<TbProduct>(sql.ToString(), dynamicParams);
        }

        public TbProduct GetProductDetail(int productId)
        {
            return dbManager.QuerySingle<TbProduct>(@"select ProdId,OrgId,ProdName,CreateTime,EditTime,Price,Score,Stock,SmallImageUrl,ImageUrl,MaxBuyCount,SaleCount,
BeginTime,EndTime,HtmlDescription,Status FROM eagles.tb_product where ProdId = @ProdId ", new {ProdId = productId});
        }
        
        public int GetOrderByProduct(int productId, int userId)
        {
            return dbManager.ExecuteScalar<int>("select count(*) from eagles.tb_order where ProdId = @ProdId and UserId = @UserId;", new { ProdId = productId, UserId = userId });
        }
    }
}